package com.itheima.health.job;

import com.itheima.health.constant.RedisConstant;
import com.itheima.health.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Set;

@Slf4j
public class ClearImgJob implements Job {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private AliOssUtil aliOssUtil;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("清除图片: " + LocalDateTime.now());

        Set<String> set = redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        log.info("需要清除的图片 = {}", set);

        if (set != null) {
            for (String picName : set) {
                aliOssUtil.deleteFileFromOss(picName);
                redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
            }
        }
    }
}
